********************************************************************************

clear all
global beg_path `"XXX"' // Indicate your working directory

set more off

********* Create a folder for datasets prepared for the merge

mkdir `"${beg_path}Data\Micro\BES\To merge"' 


******** 1964-1970

use `"${beg_path}Data\Micro\BES\STATA63-70PoliticalChangeinBritainData.dta"', clear

codebook v938 v939 v940 v941, c
fre  v938 v939 v940 v941
gen yob = 1963-v938 if v938 !=0
replace yob = 1964-v939 if v939!=0 & yob==.
replace yob = 1966-v940 if v940!=0 & yob==.
replace yob = 1970-v941 if v941!=0 & yob==.
gen id_var = v2

preserve 
gen election_year = 1964 
recode v356 (5=0 "abstention") (1 = 1 "voted") (else =.), gen(participation) // See p. 143 in the codebook 
keep id_var election_year yob participation
keep if yob !=. & participation !=. 
save `"${beg_path}Data\Micro\BES\To merge\1964_to_merge.dta"', replace
restore 

preserve 
gen election_year = 1966 
recode v357 (5=0 "abstention") (1 = 1 "voted") (else =.), gen(participation) 
keep id_var election_year yob participation
keep if yob !=. & participation !=. 
save `"${beg_path}Data\Micro\BES\To merge\1966_to_merge.dta"', replace
restore 

preserve 
gen election_year = 1970 
recode v358 (5=0 "abstention") (1 = 1 "voted") (else =.), gen(participation) 
keep id_var election_year yob participation
keep if yob !=. & participation !=. 
save `"${beg_path}Data\Micro\BES\To merge\1970_to_merge.dta"', replace
restore 

******** 1974 (February)

use `"${beg_path}Data\Micro\BES\74feb.dta"', clear

recode feb114 (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace feb191 = . if inlist(feb191, 0, 1000)

gen yob = feb191
gen election_year = 197402
gen id_var = feb001

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\1974feb_to_merge.dta"', replace

******** 1974 (October)

use `"${beg_path}Data\Micro\BES\74oct.dta"', clear

recode oct114 (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace oct191 = . if inlist(oct191, 0)

gen yob = oct191
gen election_year = 197010
gen id_var = respno

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\1974oct_to_merge.dta"', replace

******** 1979

use `"${beg_path}Data\Micro\BES\79BESS.dta"', clear

recode m114145 (5=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace m191279 = . if m191279 ==0 

gen yob = m191279
gen election_year = 1979
gen id_var = m001001b
destring id_var, replace 
keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\1979_to_merge.dta"', replace

******** 1983

use `"${beg_path}Data\Micro\BES\83BES.dta"', clear
recode q7a (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace q56 = . if q56 == 99

gen yob = 1983 - q56 
gen election_year = 1983
gen id_var = id1


keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\1983_to_merge.dta"', replace

*** 1987

use `"${beg_path}Data\Micro\BES\87BES.dta"', clear

recode v6a (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace v58c = . if inlist(v58c, 98, 99) 

gen yob = 1987 - v58c 
gen election_year = 1987
gen id_var = serialno

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\1987_to_merge.dta"', replace

******** 1992

use `"${beg_path}Data\Micro\BES\92BES.dta"', clear

recode v7a (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace v915c = . if inlist(v915c, 98, 99) 

gen yob = 1992 - v915c 
gen election_year = 1992
gen id_var = serialno

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\2000_to_merge.dta"', replace

***** 1997

use `"${beg_path}Data\Micro\BES\97BES.dta"', clear

recode voted (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)

replace rage = . if inlist(rage, 98, 99) 

gen yob = 1997 - rage
gen election_year = 1997
gen id_var = serialno

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\1997_to_merge.dta"', replace

***** 2001

use `"${beg_path}Data\Micro\BES\2001_prepostagg.dta"', clear

recode bq8a (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)  
replace aq38 = . if inlist(aq38, 99) 

gen yob = 2001 - aq38
gen election_year = 2001
gen id_var = arespid 

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\2001_to_merge.dta"', replace

***** 2005

use `"${beg_path}Data\Micro\BES\2005BESb.dta"', clear

recode bq12a (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation)  

replace aq48 = . if inlist(aq48, 98, 99, 999) 

gen yob = 2005 - aq48
gen election_year = 2005
gen id_var = serialno

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\2005_to_merge.dta"', replace

***** 2010

use `"${beg_path}Data\Micro\BES\2010BESprepost.dta"', clear

recode bq12_1 (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation) 

replace aq63 = . if inlist(aq63, -2) 

gen yob = 2010 - aq63
gen election_year = 2010
gen id_var = cserial

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\2010_to_merge.dta"', replace

***** 2015

use `"${beg_path}Data\Micro\BES\bes_f2f_2015_v4.0.dta"', clear

recode b01 (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation) 

replace Age	= . if inlist(Age, -1) 

gen yob = 2015 - Age
gen election_year = 2015
gen id_var = serial

keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\2015_to_merge.dta"', replace

***** 2017

use `"${beg_path}Data\Micro\BES\bes_f2f_2017_v1.3.dta"', clear

recode b01 (2=0 "abstention") (1/1 = 1 "voted") (else =.), gen(participation) 

replace Age	= . if inlist(Age, -2) 

gen yob = 2017 - Age
gen election_year = 2017
gen id_var = serial
destring id_var, replace 
keep id_var election_year yob participation
keep if yob !=. & participation !=.
save `"${beg_path}Data\Micro\BES\To merge\2017_to_merge.dta"', replace

